﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Video;
namespace XRPlayer
{

    public class XRPlayerControl : MonoBehaviour
    {
        [HideInInspector]
        public float m_Volume = 100;
        public float m_CurrentVolume = 100;//Origin value is 0-1,convert the range 0-100
        [HideInInspector]
        public float m_Time;
        [HideInInspector]
        public float m_CurrentTime;
        public Slider m_TimeSlider;
        public Slider m_VolumeSlider;
        public GameObject m_VideoCanvas;

        public Text m_VolumeText;

        public Text m_TimeText;
        public Text m_CurrentTimeText;

        public VideoPlayer videoPlayer;
        public AudioSource audioPlayer;

        private bool clickBeginOnVideoSlider = false;

        private void Update()
        {
            if (videoPlayer.isPlaying && !clickBeginOnVideoSlider)
            {
                ShowCurrentTimeSlider();
            }
            ShowTimeValue();
            
            ShowVolumValue();
        }
        //private void LateUpdate()
        //{
        //}
        public void OnPlay()
        {
            videoPlayer.Play();
        }

        public void OnPause()
        {
            videoPlayer.Pause();
        }

        public void OnReview()
        {
            videoPlayer.Stop();
            videoPlayer.Play();
        }

        public void OnNext()
        {
        }

        public void OnBack()
        {
        }

        public void OnTimeChange()
        {
            if (videoPlayer.isPlaying)
            {
                clickBeginOnVideoSlider = false;
                m_CurrentTime = (float)videoPlayer.clip.length * m_TimeSlider.value;
                videoPlayer.time = m_CurrentTime;
            }
            //Judge the Current player status
        }
        public void OnTimeSliderDrag()
        {
            clickBeginOnVideoSlider = true;
            m_CurrentTime = (float)videoPlayer.clip.length * m_TimeSlider.value;
            videoPlayer.time = m_CurrentTime;
        }

        public void OnTimeSliderEndDrag()
        {
            if (clickBeginOnVideoSlider)
            {
                if (videoPlayer.isPlaying)
                {
                    m_CurrentTime = (float)videoPlayer.clip.length * m_TimeSlider.value;
                    videoPlayer.time = m_CurrentTime;
                }
                clickBeginOnVideoSlider = false;
            }

        }
        public void OnSoundOff()
        {
            audioPlayer.mute = true;
        }
        public void OnSoundOn()
        {
            audioPlayer.mute = false;
        }

        public void OnVolumChange()
        {
            m_CurrentVolume = (int)(m_VolumeSlider.value * 100);
            audioPlayer.volume = m_VolumeSlider.value;
        }

        public void OnPlaySpeedChange()
        {
            //VideoPlayer.playbackSpeed
        }
        private void ShowTimeValue()
        {
            m_Time = (float)videoPlayer.clip.length;
            m_CurrentTime = (float)videoPlayer.time;
            
            m_TimeText.text = CalculateTimeValue(m_Time).ShowTimeString;
            m_CurrentTimeText.text = CalculateTimeValue(m_CurrentTime).ShowTimeString;
        }
        private void ShowVolumValue()
        {
            m_VolumeText.text = m_CurrentVolume.ToString();
        }

        private void ShowCurrentTimeSlider()
        {
            float valueSilder = (float)(m_CurrentTime / m_Time);
            m_TimeSlider.value = valueSilder;
            
        }
        private VideoTime CalculateTimeValue(float InputSecondTime)
        {
            VideoTime TempVideoTime;
            TempVideoTime.second = (int)InputSecondTime % 60;
            TempVideoTime.minute = (int)InputSecondTime % 3600 / 60;
            TempVideoTime.hour = (int)InputSecondTime / 3600;
            TempVideoTime.ShowTimeString = string.Format("{0:D2}:{1:D2}:{2:D2}", TempVideoTime.hour,
                TempVideoTime.minute, TempVideoTime.second);
            return TempVideoTime;
        }
        /// <summary>
        /// Get the video
        /// </summary>
        public void GetVideoClip()
        {
            
        }
    }
}